var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      {
        link: '/pages/index/index',
        url: 'https://tiaonali.com/zhiwei1.jpg'
      }, {
        link: '/pages/news/index',
        url: 'https://tiaonali.com/zhiwei2.jpg'
      }
    ],
    isDefTap: true,
    hasUserInfo: false,
    matchPage: {
      loading: false,
      pageNum: 0,
      now_page_no: 1,
      data: [],
      noMore: false,
    },
    positionPage: {
      loading: false,
      pageNum: 0,
      now_page_no: 1,
      data: [],
      noMore: false,
    },
    currentTab: 0,
    banner: {},
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    isHaveResume: null,
    nonet: true ,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (app.globalData.userInfo != null) {
      that.setData({ hasUserInfo: true });
    }
    this.resetData(true);
    util.post(app.api.getAdcarouse, { type: 1 }, function (res) {
      //获取首页轮播图
      that.setData({
        banner: res.data 
      });
    });
    //是否注册过有资料

  },
  //职位详情
  positionDetailTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id
    // 切换页面
    wx.navigateTo({
      url: '../position-detail/position-detail?pid=' + id
    });
  },
  
  //我想知道
  knowTap: function (event) {
    wx.navigateTo({
      url: '../opinion/opinion'
    });
  },
  //去看看
  goTap: function (event) {
    wx.navigateTo({
      url: '../home/home'
    });
  },
  //去完善
  perfectTap: function (event) {
    wx.reLaunch({
      url: '/pages/web-view/web-view',
    })
  },
  //去登陆
  registerTap: function (event) {
    var that=this;
  
    if (that.data.isHaveResume == null || that.data.isHaveResume == 'binded' || that.data.isHaveResume == 'generated') {
      wx.showModal({
        title: '请先登录',
        content: '请微信授权登录,并完善您的简历',
        cancelText: '取消',
        cancelColor: '#999',
        confirmText: '确认',
        confirmColor: '#4990E2',
        success: function (res) {
          if (res.confirm) {
            wx.reLaunch({
             url: '/pages/user/index'
             
              //url: '/pages/web-view/web-view'
              // url: '/pages/edit-resume-base/edit-resume-base?type=0',
            });
          } else if (res.cancel) {
          }
        }
      })
    } else {
      wx.reLaunch({
        url: '/pages/user/index',
      })
    }
  },
  /** 
 * 点击tab切换 
 */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      });
      var isDefTap = true;
      if (e.target.dataset.current != 0) {
        isDefTap = false;
      }
      that.setData({
        isDefTap: isDefTap
      });
    }
  },
  resetDataForPosition: function (url, now_page_no, isappend) {
    var that = this;
    var currObj = that.data.positionPage;
    util.post(url, { page: now_page_no, size: 5 }, function (res) {
      if (res.data == null) {
        wx.showToast({
          title: '正在加载中......',
          icon: 'loading',
          duration: 1200
        })
        return;
      }
      if (isappend) {
        //上拉加载更多
        that.setData({ 'positionPage.loading': false });
        now_page_no = now_page_no + 1;
        var data = [];
        for (var item in currObj.data) {
          data.push(currObj.data[item]);
        }
        for (var item in res.data.data) {
          data.push(res.data.data[item]);
        } 

      } else {
        that.setData({ 'positionPage.noMore': false });
        //下拉刷新 此处停止刷新效果  重置now_page_no为1
        now_page_no = 1;
        wx.stopPullDownRefresh();
        var data = res.data.data;
        //wx.showToast({ title: '刷新了' });
      }
      that.setData({ 'positionPage.pageNum': res.data.pageNum });
      that.setData({ 'positionPage.data': data, 'positionPage.now_page_no': now_page_no });
    });
  },

  resetDataForMatch: function (url, now_page_no, isappend) {
    var that = this;
    var currObj = that.data.positionPage;
    var unionid = wx.getStorageSync('unionid');
    util.post(url, { unionid: unionid,page: now_page_no, size: 20 }, function (res) {
      if (res.data == null) {
        wx.showToast({
          title: '正在加载',
          icon: 'loading',
          duration: 1200
        })
        return;
      }
      if (isappend) {
        //上拉加载更多
        that.setData({ 'matchPage.loading': false });
        now_page_no = now_page_no + 1;
        var data = [];
        for (var item in currObj.data) {
          data.push(currObj.data[item]);
        }
        for (var item in res.data.data) {
          data.push(res.data.data[item]);
        }

      } else {
        that.setData({ 'matchPage.noMore': false });
        //下拉刷新 此处停止刷新效果
        now_page_no = 1;
        wx.stopPullDownRefresh();
        var data = res.data.data;
        //wx.showToast({ title: '刷新了' });
      }
      that.setData({ 'matchPage.pageNum': res.data.pageNum });
      that.setData({ 'matchPage.data': data, 'matchPage.now_page_no': now_page_no });
    });
  },
  resetData: function (isOnLoad, isappend, now_page_no) {
    var isappend = isappend == undefined ? false : true;
    var now_page_no = now_page_no == undefined ? 1 : now_page_no;
    // var url = "";
    // if (isOnLoad) {
      //如果是onload函数中执行此函数，那么resetDataForPosition和resetDataForMatch都要执行
     // this.resetDataForPosition(app.api.getJob, now_page_no, isappend);
      this.resetDataForMatch(app.api.getList, now_page_no, isappend);
    // } else {
    //   if (this.data.currentTab == 0) {
    //     //最新职位
    //    // url = app.api.getJob;
    //     //this.resetDataForPosition(url, now_page_no, isappend);
    //   } else {
    //     //精准匹配
    //     url = app.api.getList;
    //     this.resetDataForMatch(url, now_page_no, isappend);
    //   }
    // }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this;
      wx.getNetworkType({
        success: function (res) {
          // 返回网络类型, 有效值：
          // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
          var networkType = res.networkType
          if (networkType == "none") {
            that.setData({
              nonet: false,
              gobtn: true,
              perfectbtn:true
            })
          }
        }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.resetData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currObj = null;
    if (this.data.currentTab == 0) {
      //最新职位
      currObj = this.data.positionPage;
    } else {
      //精准匹配
      currObj = this.data.matchPage;
    }

    if (currObj.loading) return;
    if (currObj.now_page_no >= currObj.pageNum) {

      if (this.data.currentTab == 0) {
        //最新职位
        this.setData({ 'positionPage.noMore': true });
      } else {
        //精准匹配
        this.setData({ 'matchPage.noMore': true });
      }

    } else {
      this.resetData(false, 1, currObj.now_page_no);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})